package com.yehun.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.List;
import java.util.function.Function;


/**
 * json辅助类
 * Created by Genius on 2017/3/27 0027.
 */
public class JsonUtils {

    private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);

    //SerializerFeature
    //QuoteFieldNames				输出key时是否使用双引号,默认为true
    //UseSingleQuotes				输出key时是否使用单引号
    //WriteDateUseDateFormat		格式化时间字段
    //WriteNullListAsEmpty			List字段如果为null,输出为[],而非null
    //WriteNullStringAsEmpty		字符类型字段如果为null,输出为"",而非null
    //WriteNullBooleanAsFalse		Boolean字段如果为null,输出为false,而非null
    //WriteNullNumberAsZero			数值字段如果为null,输出为0,而非null
    //WriteMapNullValue				是否输出值为null的字段,默认为false
    //DisableCheckSpecialChar		一个对象的字符串属性中如果有特殊字符如双引号，将会在转成json时带有反斜杠转移符。如果不需要转义，可以使用这个属性。默认为false
    public static SerializerFeature[] serializables = {
            SerializerFeature.WriteClassName,
            SerializerFeature.WriteEnumUsingToString,
            SerializerFeature.WriteDateUseDateFormat, //格式化日期
            SerializerFeature.WriteMapNullValue, //空值字段输出
            SerializerFeature.WriteNullStringAsEmpty, //字符串为null时输出""
            SerializerFeature.WriteNullListAsEmpty, //列表为null时输出[]
            SerializerFeature.WriteNullBooleanAsFalse, //boolean类型为null时输出false
            SerializerFeature.WriteNullNumberAsZero //整型为null时输出0
    };

    public static JSONObject toObject(String json) {
        return JSON.parseObject(json);
    }

    public static <T> T toObject(String json, Class<T> type) {
        return JSON.parseObject(json, type);
    }

    public static Object toObject(byte[] bytes) {
        return JSON.parse(bytes);
    }

    public static JSONArray toList(String json) {
        return JSON.parseArray(json);
    }

    public static <T> List<T> toList(String json, Class<T> type) {
        return JSON.parseArray(json, type);
    }

    public static byte[] toBytes(Object object) {
        return JSON.toJSONBytes(object);
    }

    public static byte[] toBytes(Object object, SerializerFeature... features) {
        return JSON.toJSONBytes(object, features);
    }


    /**
     * 将Json对象转换成Object
     * @param json json字符串
     * @return Object
     */
    public static Object deserialize(String json) {
        return JSON.parse(json);
    }

    /**
     * 对象转json
     * @param object 对象
     * @param features 参数
     * @return json字符串
     */
    public static String serialize(Object object, SerializerFeature... features) {

        //new SimplePropertyPreFilter(实体类.class, "字段名","字段名"); 过滤多余的字段
        //return JSON.toJSONString(object,
        //		SerializerFeature.WriteDateUseDateFormat,
        //		SerializerFeature.WriteNullListAsEmpty,
        //		SerializerFeature.WriteNullStringAsEmpty,
        //		SerializerFeature.WriteNullBooleanAsFalse,
        //		SerializerFeature.WriteMapNullValue);
        return JSON.toJSONString(object, features);
    }

    public static void main(String[] args) {
        String string = "[{\"name\":\"a\"},{\"name\":\"b\"}]";
        String json = JSON.toJSONString(string);
        logger.info("json => " + json);
        //JSONArray list = toList(string);
        //List<TestS> list = toList(string, TestS.class);

        Object list = deserialize(string);
        logger.info("len => " + list.getClass().getName());
        //logger.info("len => " + list.size());
        //for (Object object : list) {
        //    TestS testS = (TestS) object;
        //    logger.info("name => " + testS.getName());
        //}
    }

    class TestS {
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

}